package com.lyf.baoxian.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.TextView;

import com.lyf.baoxian.R;
import com.lyf.baoxian.utils.PackageInfoUtils;

/**
 * Created by 刘亚飞 on 2016/11/30.
 */

public class SplashActivity extends Activity {
    private TextView tv_version;
    private ImageView ivSplash;
    //当前版本号
   private String currentVersion;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        tv_version = (TextView) findViewById(R.id.tv_version);
        ivSplash = (ImageView) findViewById(R.id.iv_splash);

        //设置渐变动画
        AlphaAnimation animation = new AlphaAnimation(0.5f,1.0f);
        //设置动画持续事件
        animation.setDuration(2000);
        //开始显示动画
        ivSplash.setAnimation(animation);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        //获取当前版本号
        currentVersion = PackageInfoUtils.getPackageVersion(this);
//        tv_version.setText("当前版本号" + currentVersion);

//        checkVersion();

        /*new Thread() {
            @Override
            public void run() {
                SystemClock.sleep(1500);
                loadMainUI();
            }
        }.start();*/
    }

    /**
     * 检测服务器版本号
     *
     * @return
     */
    private void checkVersion() {
        //开启子线程获取服务器版本号
        new Thread() {
            @Override
            public void run() {
               //访问服务器,服务器版本号与当前版本号做对比，如果服务器版本号大于当前
                //版本号，则更新版本
                //假设当前服务器版本号为1
                String serverVersion = 1.0+"";
                if (currentVersion.equals(serverVersion)) {
                    loadMainUI();
                }else {
                    //更新版本
                }
            }
        }.start();


    }

    /**
     * 进入登陆界面
     */
    private void loadMainUI() {
        Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
        startActivity(intent);
        finish();
    }
}
